import store from "../../utils/store"
import { getLocation, valIsNull } from "../../utils/utils"
import dateUtils from "../../utils/dateUtils"

const pageOptions = {
	// 页面数据
	data: {
		isFirstOnShow: true, // 是否为首次执行onShow
		location: {
			longitude: 116.4044423429857,
			latitude: 39.90041259188714,
		},
		formatted_address: "",
		// 地理位置限制
		locationLimit: {
			centerLatitude: "",
			centerLongitude: "",
			distance: 0,
			limit: false,
		},
		currentDate: "",
	},
	// 页面载入时
	onLoad(e) {
		// 获取当前日期
		const currentDateObj = dateUtils.getDateObj(new Date())
		this.setData({
			currentDate: `${currentDateObj.year}.${currentDateObj.month}.${currentDateObj.day}    ${currentDateObj.week}`,
		})
		
		this.init(e)
	},
	// 页面初始化
	init(e) {
		// 获取地理位置限制
		this.setData({
			locationLimit: store.locationLimit.get(),
		})
		// 获取位置
		this.getLocation()
	},
	// 页面准备好时
	onReady() {},
	// 页面显示时
	onShow() {
		const { isFirstOnShow } = this.data

		if (isFirstOnShow) {
			// 首次执行时
			this.setData({
				isFirstOnShow: false,
			})
			return
		}
	},
	// 页面隐藏时
	onHide() {},
	// 页面卸载时
	onUnload() {},
	// 下拉页面时
	onPullDownRefresh() {},
	// 到达页面底部时
	onReachBottom() {},
	// 页面滚动时
	onPageScroll() {},
	// 分享时，注：onShareAppMessage不能为async异步函数，会导致不能及时取得返回值，使得分享设置无效
	onShareAppMessage() {
		/* const title = ''
    const path = ''
    const imageUrl = ``

    return {
      title,
      path,
      imageUrl,
    } */
	},
	getLocation() {
		getLocation(
			this.data.locationLimit && this.data.locationLimit.limit
				? this.data.locationLimit
				: null,
		).then((res) => {
			const { location, formatted_address } = res
			this.setData({
				location,
				formatted_address,
			})
			
			store.location.set(JSON.stringify(location))
		})
	},
	nextStep() {
		if (valIsNull(store.location.get())) {
			wx.showToast({
				title: "请先获取位置",
				icon: "none",
			})
			return
		} else {
			//需要验证身份证则跳转到身份证验证页面
			if (store.needSfzImageUpload.get()) {
				wx.redirectTo({
					url: "/informationVerification/pages/uploadIdCard/index",
				})
				return
				//跳转到人脸验证页面
			} else {
				wx.redirectTo({
					url: "/informationVerification/pages/photoInstructions/index",
				})
				return
			}
		}
	},
}

Page(pageOptions)
